package service

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto/rpc"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func PublishVideo(c *gin.Context) {
	var form request.PublishVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	if form.Title == "" || len(form.Title) < 5 || len(form.Title) > 100 {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "标题必填项长度限制"})
		return
	}
	video, err := config.VideoRpcClient.PublishVideo(c, &__.PublishVideoReq{
		UserId:   form.UserId,
		Title:    form.Title,
		Category: form.Category,
		Tags:     form.Tags,
		CoverUrl: form.CoverUrl,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "视频发布失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "视频发布成功", "data": gin.H{
		"videoId": video.VideoId,
	}})
	return
}

func CheckVideo(c *gin.Context) {
	var form request.CheckVideo
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	checkVideo, err := config.VideoRpcClient.CheckVideo(c, &__.CheckVideoReq{
		UserId:  form.UserId,
		VideoId: form.VideoId,
		Status:  form.Status,
		Reason:  form.Reason,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "视频审核失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "视频审核完成", "data": gin.H{
		"checkVideoId": checkVideo.VideoId,
		"videoToken":   checkVideo.VideoToken,
	}})
	return
}
